package com.toolmanrln.common.util;

import com.alibaba.fastjson.JSON;
import com.toolmanrln.common.api.ResponseData;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author ToolMan.RLN
 * @since 2020-05-25 9:53
 */
public final class ResponseUtil {

    private ResponseUtil() {
        throw new UnsupportedOperationException("This class should not be instantiated.");
    }

    public static void responseToJson(HttpServletResponse response, ResponseData responseData) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        PrintWriter writer = response.getWriter();
        writer.println(FastJsonUtil.toJSONWithFeatures(responseData));
        writer.flush();
        writer.close();
    }
}
